package service

import (
	"admin100/app/dao/centerdao"
	"admin100/app/dao/chargedao"
	"admin100/app/dao/gamedao"
	"admin100/app/dao/internal_game"
	"admin100/app/model/centermodel"
	"admin100/app/model/gamemodel"
	"admin100/app/service/centerservice"
	"admin100/library/define"
	"admin100/library/define/defErr"

	"github.com/gogf/gf/frame/g"
)

var Player = &playerService{}

type playerService struct{}

func (s *playerService) AccountDetail(req *gamemodel.AccountDetailReq) []*centermodel.GlobalPlayer {
	data, err := centerdao.GlobalPlayer.Where(g.Map{
		centerdao.GlobalPlayer.Columns.PlatformId: req.PlatformId,
		centerdao.GlobalPlayer.Columns.Account:    req.Account,
	}).FindAll()
	defErr.SelectRetPanic(err, data, define.DataOptFail)

	for _, v := range data {
		detail := s.Detail(&gamemodel.DetailReq{
			PlatformId: v.PlatformId,
			ServerId:   v.ServerId,
			PlayerId:   int(v.Id),
		})
		if detail != nil {
			v.Level = detail.Level
			v.VipLevel = detail.VipLevel
			v.TotalChargeMoney = int(detail.TotalChargeMoney)
		}
	}

	return data
}
func (s *playerService) Detail(req *gamemodel.DetailReq) *gamemodel.PlayerDetail {

	var (
		playerId     = req.PlayerId
		playerDetail = &gamemodel.PlayerDetail{}
		node         = centerservice.CGameServer.FetchNodeStrByPtIdAndSid(req.PlatformId, req.ServerId)
	)

	if node == "" {
		return nil
	}
	var (
		playerDb     = gamedao.NewPlayerDao(node)
		playerPropDb = gamedao.NewPlayerPropDao(node)
	)

	err := playerDb.Fields("player.*", "player_data.*", "player_task.task_id, player_vip.level as vip_level").
		LeftJoin("player_data", "player_data", "player.id = player_data.player_id").
		LeftJoin("player_task", "player_task", "player.id = player_task.player_id").
		LeftJoin("player_vip", "player_vip", "player_vip.player_id = player.id").Scan(playerDetail)
	defErr.SelectRetPanic(err, playerDetail, define.DataOptFail)

	playerDetail.PlayerPropList = playerPropDb.GetPlayerPropList(playerId)
	playerDetail.Player.Nickname = playerDetail.Player.ServerId + "." + playerDetail.Player.Nickname

	playerDb.GetPlayerCommonListByTNames(playerId, internal_game.TableNames, playerDetail)

	// playerDetail.FactionName = playerDb.GetPlayerFactionName(playerId)

	playerChargeData := chargedao.PlayerChargeInfoRecord.GetPlayerChargeDataOne(playerId)
	if playerChargeData != nil {
		playerDetail.TotalChargeMoney = int(playerChargeData.TotalMoney)
	}

	globalAccount := centerdao.GlobalAccount.GetGlobalAccount(req.PlatformId, playerDetail.Player.AccId)
	if globalAccount != nil {
		playerDetail.Player.Type = globalAccount.Type
		playerDetail.Player.AccountForbidType = globalAccount.ForbidType
		playerDetail.Player.AccountForbidTime = globalAccount.ForbidTime
	}

	playerDetail.ExtraData = playerDb.GetPlayerExtraData(playerId)
	return playerDetail
}
